package com.youtellv1.controller;

import com.youtellv1.config.ResultData;
import com.youtellv1.entity.Admin;
import com.youtellv1.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/manager/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/list")
    public ResultData<List<Admin>> list() {
        try {
            List<Admin> adminList = adminService.findAllService();
            return new ResultData<>(adminList);
        } catch (Exception e) {
            return new ResultData<>(500, "获取管理员列表失败：" + e.getMessage(), null);
        }
    }

    @GetMapping("/get/{aid}")
    public ResultData<Admin> get(@PathVariable Integer aid) {
        try {
            if (aid == null) {
                return new ResultData<>(400, "管理员ID不能为空", null);
            }
            Admin admin = new Admin();
            admin.setAid(aid);
            Admin result = adminService.findAllService(admin);
            if (result == null) {
                return new ResultData<>(404, "未找到该管理员", null);
            }
            return new ResultData<>(result);
        } catch (Exception e) {
            return new ResultData<>(500, "获取管理员信息失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/save")
    public ResultData<Void> save(@RequestBody Admin admin) {
        try {
            if (admin == null) {
                return new ResultData<>(400, "管理员信息不能为空", null);
            }
            if (!StringUtils.hasText(admin.getAccount())) {
                return new ResultData<>(400, "账号不能为空", null);
            }
            if (!StringUtils.hasText(admin.getPassword())) {
                return new ResultData<>(400, "密码不能为空", null);
            }
            adminService.saveService(admin);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "保存管理员失败：" + e.getMessage(), null);
        }
    }

    @PutMapping("/update")
    public ResultData<Void> update(@RequestBody Admin admin) {
        try {
            if (admin == null || admin.getAid() == null) {
                return new ResultData<>(400, "管理员ID不能为空", null);
            }
            adminService.updateService(admin);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "更新管理员失败：" + e.getMessage(), null);
        }
    }

    @DeleteMapping("/delete/{aid}")
    public ResultData<Void> delete(@PathVariable Integer aid) {
        try {
            if (aid == null) {
                return new ResultData<>(400, "管理员ID不能为空", null);
            }
            Admin admin = new Admin();
            admin.setAid(aid);
            adminService.deleteService(admin);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "删除管理员失败：" + e.getMessage(), null);
        }
    }
}